package com.hk.yajin.function.start;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.EdgeEffectCompat;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hk.developmentlib.utills.BitmapHelper;
import com.hk.developmentlib.utills.PreferUtil;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.developmentlib.utills.constants.PreferConstant;
import com.hk.yajin.R;
import com.hk.yajin.base.BaseActivity;
import com.hk.yajin.function.MainActivity;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述：引导页的Activity
 * 创建人：huj
 * 创建时间：2016/4/19 9:43
 */
public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener, View.OnClickListener {


    private List<ImageView> imageViewList;
    private int basicWidth = UiUtil.getDimens(R.dimen.dp15); // 点与点之间的距离

    private LinearLayout llPointGroup; // 点的组
    private View mSelectedPoint;
    private TextView btnStartExperience;

    private EdgeEffectCompat rightEdge;

    //原点的直径
    int guidePointBulk = UiUtil.getDimens(R.dimen.dp7);
    //移动原点的属性
    RelativeLayout.LayoutParams pointParams = new RelativeLayout.LayoutParams(guidePointBulk, guidePointBulk);

    //引导页数量以及显示的图片资源id
//    int[] imageIDs = {
//            R.color.red_e53434,
//            R.color.red_e53434,
//            R.color.red_e53434,
//            R.color.red_e53434
//    };
    int[] imageIDs = {
            R.mipmap.yindao1,
            R.mipmap.yindao2,
            R.mipmap.yindao3
    };
    private View selectedPointRl;
    private int leftMargin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
    }


    /**
     * 初始化控件
     */
    private void initView() {
        ViewPager mViewPager = (ViewPager) findViewById(R.id.vp_guide);
        btnStartExperience = (TextView) findViewById(R.id.btn_guide_start_experience);
        llPointGroup = (LinearLayout) findViewById(R.id.ll_guide_point_group);
        mSelectedPoint = findViewById(R.id.v_guide_selected_point);
        selectedPointRl = findViewById(R.id.v_guide_selected_point_rl);

        mSelectedPoint.setBackgroundDrawable(BitmapHelper.createDrawable(Color.WHITE, Color.WHITE, 100, 1));

        btnStartExperience.setBackgroundDrawable(BitmapHelper.createDrawable(Color.WHITE, Color.WHITE, 200, 2));

        imageViewList = new ArrayList<>();

        GradientDrawable drawable = BitmapHelper.createDrawable(Color.TRANSPARENT, Color.WHITE, 100, 2);

        ImageView iv;
        View view;
        for (int i = 0; i < imageIDs.length; i++) {
            iv = new ImageView(this);
            iv.setBackgroundResource(imageIDs[i]);
            imageViewList.add(iv);

            // 向LinearLayout中添加一个点的控件
            view = new View(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(guidePointBulk, guidePointBulk);
            if (i != 0) {
                //点与点之间的间隔
                params.leftMargin = basicWidth;
            }
            view.setLayoutParams(params);
            view.setBackgroundDrawable(drawable);
            llPointGroup.addView(view);
        }

        // 把Adapter适配器和ViewPager关联起来
        GuideAdapter mAdapter = new GuideAdapter();
        mViewPager.setAdapter(mAdapter);
        mViewPager.addOnPageChangeListener(this);
        btnStartExperience.setOnClickListener(this);

        initEdgeEffectCompat(mViewPager);
    }


    private void initEdgeEffectCompat(ViewPager viewPager) {
        try {
            Field rightEdgeField = viewPager.getClass().getDeclaredField("mRightEdge");
            if (rightEdgeField != null) {
                rightEdgeField.setAccessible(true);
                rightEdge = (EdgeEffectCompat) rightEdgeField.get(viewPager);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 把对应position位置的Imageview在ViewPager中移除掉
            container.removeView(imageViewList.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            // 向ViewPager中添加一个ImageView对象, 并且在此方法中把那个被添加的imageView对象返回
            ImageView imageView = imageViewList.get(position);
            container.addView(imageView);
            return imageView;
        }
    }


    /**
     * 当滚动时
     * position 目前被选中的页面的索引
     * positionOffset 移动到下一个页面的比例, 范围是: 0.0 ~ 1.0
     */
    @Override
    public void onPageScrolled(int position, float positionOffset,
                               int positionOffsetPixels) {

        leftMargin = (int) ((position + positionOffset) * (basicWidth + guidePointBulk));
        pointParams.leftMargin = leftMargin;
        mSelectedPoint.setLayoutParams(pointParams);
    }

    /**
     * 当新的页面被选中时
     */
    @Override
    public void onPageSelected(int position) {
        if (position == imageViewList.size() - 1) {
            btnStartExperience.setVisibility(View.VISIBLE);
            selectedPointRl.setVisibility(View.GONE);
        } else {
            btnStartExperience.setVisibility(View.GONE);
            selectedPointRl.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 当页面滚动的状态改变时
     */
    @Override
    public void onPageScrollStateChanged(int state) {
        //到了最后一张并且还继续拖动，出现蓝色限制边条了
        if (rightEdge != null && !rightEdge.isFinished()) {
            onClick(btnStartExperience);
        }
    }

    @Override
    public void onClick(View v) {
        // 把是否打开主页面的值标志为true, 后面就不会再打开引导页面
        PreferUtil.putBoolean(PreferConstant.FIRST_USE, true);
        //跳转到首页
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    public void onBackPressed() {

    }
}
